package test.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import junit.framework.TestCase;

import org.junit.Test;

import test.service.BaseTest;
import blog.core.exception.FormParamErrorException;
import blog.core.exception.NotFoundException;
import blog.spring.db.entity.CmsUser;
import blog.spring.db.entity.User;
import blog.spring.db.service.IUserService;

public class UserService extends BaseTest<User> {

	@Resource
	private IUserService userService;
	@Override
	public void init() {
			entity = new User();
			entity.setUserName("小明");
			entity.setPassWord("我是密码");
			CmsUser user = new CmsUser();
			user.setSource(1);
			entity.setCmsUser(user);
	}

	@Override
	public void a_save() throws FormParamErrorException {
		userService.save(entity);
	}

	@Override
	public void b_findByName() {
		TestCase.assertNotNull(entity = userService.findByName(entity.getUserName()));
	}

	@Override
	public void c_findById() throws NotFoundException {
		TestCase.assertNotNull(userService.findById(userService.findByName(entity.getUserName()).getUserId()));
	}

	@Override
	public void d_update() throws FormParamErrorException, NotFoundException {
		entity = userService.findByName(entity.getUserName());
		entity.setUserName("我不叫小明");
		userService.update(entity);
	}

	@Override
	public void e_remove() {
		userService.remove(entity);
	}

	@Override
	public void f_addMulti() throws FormParamErrorException {
		for (int i = 0; i < 50; i++) {
			entity = new User();
			entity.setUserName("小明"+i);
			userService.save(entity);
		}
	}

	@Override
	public void g_findList() {
		TestCase.assertFalse(userService.findList(1, 10, null, null).size() <= 0);
	}

	@Override
	public void h_searchList() {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("userName", "小明");
		TestCase.assertTrue(userService.searchList(conditions, 1, 10, null, null, true).size() > 0);
	}

	@Override
	public void i_searchUnique() {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("userName", "小明");
		TestCase.assertTrue(userService.searchUnique(conditions, true) != null);
	}

	@Test
	public void j_login() throws FormParamErrorException{
		TestCase.assertNotNull(userService.login(entity.getUserName(), entity.getPassWord()+"1231"));
	}


}
